package com.byxy.xyqas.controller;

import com.byxy.xyqas.service.ForumService;
import com.byxy.xyqas.service.TabService;
import com.byxy.xyqas.vo.TabVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TabController {
	@Autowired
	private TabService tabService;

	@Autowired
	private ForumService forumService;

	@GetMapping("toTabManagePage.do")
	public String findAll(Model model) {
		model.addAttribute("tabs", tabService.findAll());
		return "tabManage";
	}

	@GetMapping("toAddTabPage.do")
	public String toAddTabPage(Model model) {
		model.addAttribute("forums", forumService.findAll());
		return "addTab";
	}

	@RequestMapping("addTab.do")
	public String addTab(@RequestParam("tab_name") String tabName, @RequestParam("selectForum") Integer forumId) {
		TabVO tabVO = new TabVO();
		tabVO.setForumId(forumId);
		tabVO.setTabName(tabName);
		tabService.add(tabVO);
		return "redirect:toTabManagePage.do";
	}

	@GetMapping("toModifyTabPage.do")
	public String toModifyTabPage(@RequestParam("tabId") Integer tabId, Model model) {
		model.addAttribute("forums", forumService.findAll());
		model.addAttribute("tab", tabService.get(tabId));
		return "modifyTab";
	}

	@RequestMapping("modifyTabInfo.do")
	public String modifyTabInfo(Model model, @RequestParam("tab_id") Integer tabId,
			@RequestParam("tab_name") String tabName, @RequestParam("selectedForumId") Integer selectedForumId) {
		model.addAttribute("tab", tabService.get(tabId));
		TabVO tabVO = new TabVO();
		tabVO.setForumId(selectedForumId);
		tabVO.setTabName(tabName);
		tabVO.setTabId(tabId);
		tabService.update(tabVO);
		return "redirect:toTabManagePage.do";
	}

	@GetMapping("disEnableTab.do")
	public String disEnableTab(@RequestParam("tabId") Integer tabId) {
		tabService.delete(tabId);
		return "redirect:toTabManagePage.do";
	}

}
